package com.nju.mini_tiktok.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.nju.mini_tiktok.PlayActivity;
import com.nju.mini_tiktok.base.BaseRecycleViewAdapter;
import com.nju.mini_tiktok.base.BaseViewHolder;
import com.nju.mini_tiktok.bean.VideoBean;
import com.nju.mini_tiktok.R;
import java.util.List;

import com.nju.mini_tiktok.listener.NoDoubleClickListener;
import com.nju.mini_tiktok.utils.Utility;
import com.nju.mini_tiktok.view.CircleImageView;

public class GridVideoAdapter extends BaseRecycleViewAdapter<VideoBean>{

    private OnListItemClickedListener onListItemClickedListener;
    private int actual_width;
    private Context context;

    public GridVideoAdapter(Context context, List<VideoBean> datas) {
        super(context, datas);
        this.context = context;
    }

    @Override
    protected int getLayout(int viewType) {
        return R.layout.item_gridvideo;
    }


    @Override
    protected void bindData(BaseViewHolder holder, VideoBean bean, int adapterPosition) {
        TextView tv_title = holder.getView(R.id.item_title);
        CircleImageView avatar = holder.getView(R.id.item_avatar);
        final TextView give_a_like_num = holder.getView(R.id.give_a_like_num);
        final ImageView give_a_like = holder.getView(R.id.give_a_like);
        final ImageView item_cover = holder.getView(R.id.item_cover);

        int itemHeight;
        if (0 == adapterPosition) {
            itemHeight = Utility.dp2px(holder.itemView.getContext(), 200);
            //高度一致则无需再次设置
            if (itemHeight != holder.itemView.getLayoutParams().height)
                holder.itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight));
        } else {
            itemHeight = Utility.dp2px(holder.itemView.getContext(), 245);
            //高度一致则无需再次设置
            if (itemHeight != holder.itemView.getLayoutParams().height)
                holder.itemView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, itemHeight));
        }

        if (actual_width <= 0)
            actual_width = (Utility.getsW((Activity) mContext) - Utility.dp2px(holder.itemView.getContext(), 8) / 2);

        //加载封面图片
        Glide.with(holder.itemView.getContext())
                .asDrawable()
                .load(bean.getCoverUrl())
                .into(item_cover);
        //加载头像
        Glide.with(holder.itemView.getContext())
                .load(bean.getUserBean().getAvatar())
                .into(avatar);

        //设置描述
        tv_title.setText(bean.getDescription());
        //设置点赞数
        give_a_like_num.setText(bean.getLikeCount()+"");
        give_a_like.setSelected(bean.isLiked());

        holder.itemView.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (null != onListItemClickedListener)
                    onListItemClickedListener.onItemClicked(adapterPosition);
            }
        });
        holder.itemView.setOnClickListener(v -> {
            PlayActivity.initPos = adapterPosition;
            context.startActivity(new Intent(context, PlayActivity.class));
        });
    }

    public void setOnListItemClickedListener(OnListItemClickedListener onListItemClickedListener) {
        this.onListItemClickedListener = onListItemClickedListener;
    }

    //回收
    @Override
    public void onViewRecycled(BaseViewHolder holder) {
        ImageView item_cover = holder.getView(R.id.item_cover);
        CircleImageView item_avatar = holder.getView(R.id.item_avatar);
        if (item_cover != null) {
            Glide.with(holder.itemView.getContext()).clear(item_cover);
        }
        if (item_avatar != null) {
            Glide.with(holder.itemView.getContext()).clear(item_avatar);
        }
    }
}
